package cvo.chess.client.entities.armies.pieces;

import java.util.ArrayList;
import java.util.List;

import cvo.chess.client.entities.IGameFacade;
import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;
import cvo.chess.client.entities.game.Army;
import cvo.chess.client.entities.game.Piece;

/**
 * 
 * @author Stijn.Van.Antwerpen
 * 
 *         A concrete Piece that represents a Pawn
 */
public class Pawn extends Piece {

	public Pawn(IGameFacade gf, Army army) {
		super(gf, army);
	}

	public Pawn(IGameFacade gf, Army army, String name) {
		super(gf, army, name);
	}

	@Override
	public List<ChessboardCoordinate> getPossibleMoves() throws Exception {
		// ============================================================================================
		// variabelen
		// ============================================================================================
		List<ChessboardCoordinate> possibleMoves = new ArrayList<ChessboardCoordinate>();
		IChessboardCoordinate currentPosition = gf.getPositionOfPiece(this);
		ChessboardCoordinate possiblePosition = null;
		ChessboardCoordinate skippedPosition = null;

		// ============================================================================================
		// move pawn 1 place forward
		// ============================================================================================
			
			if (this.getColor().equalsIgnoreCase("WHITE")) {
				possiblePosition = new ChessboardCoordinate(gf.getBoard(),
					currentPosition.getCol(), currentPosition.getRow() - 1);
				if (gf.getPieceAt(possiblePosition) == null) {
					possibleMoves.add(possiblePosition);
				}
			} else if (this.getColor().equalsIgnoreCase("BLACK")) {
				possiblePosition = new ChessboardCoordinate(gf.getBoard(),
						currentPosition.getCol(), currentPosition.getRow() + 1);
				if (gf.getPieceAt(possiblePosition) == null) {
					possibleMoves.add(possiblePosition);
				}
			}

		// ============================================================================================
		// move pawn 2 places forward at start
		// ============================================================================================
			if (this.getColor().equalsIgnoreCase("WHITE")) {
				if (currentPosition.getRow() == 6) { // 6 staat voor de 2de rij
														// op het bord (=start
														// positie van witte
														// pion)
					possiblePosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol(),
							currentPosition.getRow() - 2);
					skippedPosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol(),
							currentPosition.getRow() - 1);
				}

				if (gf.getPieceAt(skippedPosition) == null
						&& gf.getPieceAt(possiblePosition) == null) {
					possibleMoves.add(possiblePosition);
				}
			} else if (this.getColor().equalsIgnoreCase("BLACK")) {
				if (currentPosition.getRow() == 1) { // 1 staat voor de 7de rij
														// op het bord (=start
														// positie van zwarte
														// pion)!
					possiblePosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol(),
							currentPosition.getRow() + 2);
					skippedPosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol(),
							currentPosition.getRow() + 1);
				}
				if (gf.getPieceAt(skippedPosition) == null
						&& gf.getPieceAt(possiblePosition) == null) {
					possibleMoves.add(possiblePosition);
				}
			}

			// ============================================================================================
			// pawn takes other piece
			// ============================================================================================
			for (int i = -1; i < 2; i += 2) {
				if (this.getColor().equalsIgnoreCase("WHITE")) {
					possiblePosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol() - i,
							currentPosition.getRow() - 1);
					if (gf.getPieceAt(possiblePosition) != null
							&& gf.getPieceAt(possiblePosition).getColor() != this
									.getColor()) {
						possibleMoves.add(possiblePosition);
					}
				} else if (this.getColor().equalsIgnoreCase("BLACK")) {
					possiblePosition = new ChessboardCoordinate(gf.getBoard(),
							currentPosition.getCol() - i,
							currentPosition.getRow() + 1);
					if (gf.getPieceAt(possiblePosition) != null
							&& gf.getPieceAt(possiblePosition).getColor() != this
									.getColor()) {
						possibleMoves.add(possiblePosition);
					}
				}
			}

			// ============================================================================================
			// move pawn "en-passant"
			// ============================================================================================
			// "EN-PASSANT" is out of scope for this project
			
			// ============================================================================================
			// return List of possible moves
			// ============================================================================================
			return possibleMoves;
		}
}
